package com.casic.ops.runner.ssh;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ops
 * @Description TODO
 * @Author mianbin
 * @Date 2022/6/22 16:03
 * @Version 1.0
 **/
@Component
public class SshClientContextImpl implements SshClientContext {

    /**
     * 存储SshClient Key:任务id value:SshClient集合
     */
    private Map<Long, List<SshClient>> sshClientMap = new HashMap<>();

    @Override
    public void addSshClient(Long taskId, SshClient sshClient) {
        List<SshClient> sshClients = sshClientMap.get(taskId);
        if (sshClients == null) {
            sshClients = new ArrayList<>();
            sshClientMap.put(taskId, sshClients);
        }
        sshClients.add(sshClient);
    }

    @Override
    public List<SshClient> getSshClient(Long taskId) {
        return sshClientMap.get(taskId);
    }

    @Override
    public void remove(Long taskId, SshClient sshClient) {
        List<SshClient> sshClients = sshClientMap.get(taskId);
        if (sshClients != null) {
            sshClients.remove(sshClient);
        }
    }
}
